package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStatMfdDayMapper;
import cc.rengu.igas.momp.common.entity.TMStatMfdDay;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 手续费减免活动单日统计表数据库操作实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/20 22:31
 */

public class TMStatMfdDayMapperImpl implements TMStatMfdDayMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用主键获取手续费减免活动单日统计表数据库操作实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdDay 手续费减免活动单日统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/20 22:49
     */
    @Override
    public TMStatMfdDay selectMchntFeeDiscountDayStatisticsByPK(String instId, String mcNo, String ruleIndex, String transDate) throws Exception {

        TMStatMfdDay tmStatMfdDay = new TMStatMfdDay();
        tmStatMfdDay.setInstId(instId);
        tmStatMfdDay.setTxnDate(transDate);
        tmStatMfdDay.setMcNo(mcNo);
        tmStatMfdDay.setRuleIndex(ruleIndex);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountDayStatisticsByPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdDay, tmStatMfdDay);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>的商户手续费优惠活动<{}>规则编号<{}>的统计数据失败!<{}>",
                    instId, mcNo, ruleIndex, iReturnCode);
            return null;
        }

        return tmStatMfdDay;
    }

    /**
     * 回退手续费减免活动单日统计表数据数据库操作实现
     *
     * @param tmStatMfdDay 手续费减免活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 19:35
     */
    @Override
    public int reverseMchntFeeDiscountDayStatistics(TMStatMfdDay tmStatMfdDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "reverseMchntFeeDiscountDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdDay, TMStatMfdDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>在<{}>的营销活动<{}>的规则<{}>的手续费减免活动单日统计表数据失败!<{}>",
                    tmStatMfdDay.getInstId(), tmStatMfdDay.getTxnDate(), tmStatMfdDay.getMcNo(), tmStatMfdDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 新增手续费减免活动单日统计表数据数据库操作实现
     *
     * @param tmStatMfdDay 手续费减免活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/10 21:02
     */
    @Override
    public int insertMchntFeeDiscountDayStatistics(TMStatMfdDay tmStatMfdDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntFeeDiscountDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdDay, TMStatMfdDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("新增机构号<{}>在<{}>的营销活动<{}>的规则<>的手续费减免活动单日统计表数据失败!<{}>",
                    tmStatMfdDay.getInstId(), tmStatMfdDay.getTxnDate(), tmStatMfdDay.getMcNo(), tmStatMfdDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 更新手续费减免活动单日统计表数据数据库操作实现
     *
     * @param tmStatMfdDay 手续费减免活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/10 21:02
     */
    @Override
    public int updateMchntFeeDiscountDayStatistics(TMStatMfdDay tmStatMfdDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntFeeDiscountDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdDay, TMStatMfdDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>在<{}>的营销活动<{}>的规则<>的手续费减免活动单日统计表数据失败!<{}>",
                    tmStatMfdDay.getInstId(), tmStatMfdDay.getTxnDate(), tmStatMfdDay.getMcNo(), tmStatMfdDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }


    /**
     * 新增手续费减免活动单日统计表记录数据库操作接口实现
     *
     * @param tmStatMfdDay 手续费减免活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/2 10:28
     */
    @Override
    public int insertTMStatMfdDay(TMStatMfdDay tmStatMfdDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompInsertTMStatMfdDay");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdDay, TMStatMfdDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>新增手续费减免活动单日统计表记录失败! RETURN_CODE=<{}>", tmStatMfdDay.getInstId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }
}
